// Author: Domien Nowicki, Bjorn Schobben

#include "TextureManagerDialog.h"
#include <QFileDialog>

TextureManagerDialog::TextureManagerDialog(QWidget *parent): QDialog(parent)
{
	setWindowTitle("Select texture");

	SetupDialog();

	setModal(true);
}

TextureManagerDialog::~TextureManagerDialog()
{
}

void TextureManagerDialog::SetupDialog()
{
	vboxLayout = new QVBoxLayout();

	textureManager = new TextureManagerView(this);
	textureManager->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);

	buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
	buttonBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	addButton = new QPushButton("Add Texture");
	addButton->setIcon(QPixmap("icons/addfile.png"));
	removeButton = new QPushButton("Remove Texture");
	removeButton->setIcon(QPixmap("icons/removefile.png"));
	emptyButton = new QPushButton("Select Empty Texture");
	emptyButton->setIcon(QPixmap("icons/emptytex.png"));

	QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
	QPushButton *cancelButton = buttonBox->button(QDialogButtonBox::Cancel);
	
	okButton->setText("Select Texture");
	okButton->setIcon(QPixmap("icons/selecttex.png"));
	cancelButton->setText("Close");
	cancelButton->setIcon(QPixmap("icons/cancel.png"));

	buttonBox->addButton(emptyButton, QDialogButtonBox::ActionRole);
	buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
	buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
	
	QObject::connect(okButton, SIGNAL(clicked()), this, SLOT(slotSelectTexture()));
	QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
	QObject::connect(addButton, SIGNAL(clicked()), this, SLOT(slotAddTexture()));
	QObject::connect(removeButton, SIGNAL(clicked()), this, SLOT(slotRemoveTexture()));
	QObject::connect(emptyButton, SIGNAL(clicked()), this, SLOT(slotEmptyTexture()));
	setLayout(vboxLayout);

	vboxLayout->addWidget(textureManager);
	
	vboxLayout->addWidget(buttonBox);

	QObject::connect(textureManager, SIGNAL(changeUserColor(unsigned int, const Pixel&)), this, SLOT(slotChangeUserColor(unsigned int, const Pixel&)));
	QObject::connect(textureManager, SIGNAL(selectTexture()), this, SLOT(slotSelectTexture()));
	QObject::connect(textureManager, SIGNAL(removeTexture()), this, SLOT(slotRemoveTexture()));
	QObject::connect(textureManager, SIGNAL(addSolidTexture()), this, SLOT(slotAddSolidTexture()));
}

// Get currently selected texture
unsigned int TextureManagerDialog::GetCurrentTextureIndex() const
{
	return currentTextureIndex;
}

// Get a texture's thumb
const QPixmap& TextureManagerDialog::GetTextureThumb(unsigned int tindex) const
{
	return textureManager->GetTextureThumb(tindex);
}

void TextureManagerDialog::slotAddTexture()
{
	// Let listeners know we want to add a texture
	emit addTexture();
}

void TextureManagerDialog::slotAddSolidTexture()
{
	// Let listeners know we want to add a solid color texture
	emit addSolidTexture();
}

void TextureManagerDialog::slotRemoveTexture()
{
	int tindex = textureManager->GetCurrentTextureIndex();

	if (tindex >= 0)
	{
		// Let listeners know we want to remove a texture
		emit removeTexture(tindex);
	}
}

// If we selected the empty texture
void TextureManagerDialog::slotEmptyTexture()
{
	currentTextureIndex = -1;

	accept();
}

TextureManagerModel* TextureManagerDialog::GetTextureManagerModel()
{
	return textureManager->GetModel();
}

void TextureManagerDialog::SetTextureManagerModel( TextureManagerModel* model )
{
	TextureManagerModel *oldModel = textureManager->GetModel();

	// Disconnect modelUpdated signal from any previous model
	if (oldModel)
	{
		oldModel->disconnect(SIGNAL(modelUpdated()));
	}

	textureManager->SetModel(model);

	// And connect to new model
	if (model)
	{
		QObject::connect(model, SIGNAL(modelUpdated()), this, SLOT(slotModelUpdated()));
	}
}

// Model was updated, check button states
void TextureManagerDialog::slotModelUpdated()
{
	CheckButtonStates();
}

void TextureManagerDialog::CheckButtonStates()
{
	if (0 == textureManager->GetTextureCount())
	{
		removeButton->setEnabled(false);
	}
	else
	{
		removeButton->setEnabled(true);
	}
}

// Select a texture
void TextureManagerDialog::slotSelectTexture()
{
	currentTextureIndex = textureManager->GetCurrentTextureIndex();

	accept();
}

// Change the user color of a texture
void TextureManagerDialog::slotChangeUserColor(unsigned int tindex, const Pixel& userColor)
{
	emit changeUserColor(tindex, userColor);
}